<script>

import * as echarts from 'echarts';

export default {
  name: 'VisOne',
  data() {
    return{

    }
  },
  mounted() {
    this.getData();
  },
  methods:{open1(msg) {
      this.$notify({
        title: msg,
        message: '这是一条成功的提示消息',
        type: 'success'
      });
    },
    open2(msg) {
      this.$notify({
        title: msg,
        message: '这是一条警告的提示消息',
        type: 'warning'
      });
    },
    open4(msg) {
      this.$notify.error({
        title: msg,
        message: '这是一条错误的提示消息'
      });
    },
    getData(){
      this.$axios.get('/api/data9', {

      }).then(res =>{

        if (res.data.code === 200) {
          let dataH = [];
          let data1 = [];
          let data11 = [];
          for (let i = 0; i < res.data.result.length; i++) {
            dataH.push(
                res.data.result[i].heightMetric
              )
            data1.push(
                res.data.result[i].count
            )
            data11.push(
                res.data.result[i].count
            )
          }
          this.$axios.get('/api/data10', {

          }).then(res =>{
            let data2 = []
            let data3 = []
            if (res.data.code === 200) {
              for (let i = 0; i < res.data.result.length; i++) {
                data2.push(
                    res.data.result[i].heightMetric
                )
                data3.push(
                     res.data.result[i].count
                )
              }
              let data_1 = data1
              let data_2 = data11
              for (let i = 0; i < dataH.length; i++) {
                for (let j = 0; j < data2.length; j++) {
                  if (dataH[i] === data2[j]){
                    data_2[i] = data3[j]
                    break;
                  }else {
                    data_2[i] = 0
                  }
                }
              }
              this.init(dataH, data_1, data_2);
            }

          }).catch(error =>{
            error = "系统出错了"
            this.open4(error)
          })
        }

      }).catch(error =>{
        error = "系统出错了"
        this.open4(error)
      })
    },
    init(dataH, data_1, data_2) {

      var chartDom = document.getElementById("main-vis-1");
      var myChart = echarts.init(chartDom);
      var option;
      option = {
        backgroundColor:"#fffdfd",
        title: {
          text: '球员身高趋势图',
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            lineStyle: {
              color: "#ddd",
            },
          },
          borderColor: 'rgba(0, 127, 105, 0.48)',
          backgroundColor: "rgba(0, 127, 105, 0.48)",
          padding: [5, 10],
          extraCssText: "box-shadow: 0 0 5px rgba(0,0,0,0.3)",
          textStyle: {
            color: "#FFFFFF", // 文字的颜色
            fontStyle: "normal", // 文字字体的风格（'normal'，无样式；'italic'，斜体；'oblique'，倾斜字体）
            fontWeight: "normal", // 文字字体的粗细（'normal'，无样式；'bold'，加粗；'bolder'，加粗的基础上再加粗；'lighter'，变细；数字定义粗细也可以，取值范围100至700）
          },
          formatter: function (params) {  //使用formatter函数修改需要的样式
            console.log('params',params)
            let res = ""
            res += '<div>' +
                '<p style="color:#fff">' + params[0].seriesName + ':' + params[0].value + '个</p>' +
                '<p style="color:#fff">' + params[1].seriesName + ':' + params[1].value + '个</p>'
                + '</div>'
            return res
          }
        },
        legend: {
          show: true,
          x: "right",
          y: "0",
          icon: "circle",
          itemWidth: 10,
          itemHeight: 10,
          textStyle: {
            color: "#444",
          },
        },
        grid: {
          left: "2%",
          right: "2%",
          top: "35%",
          bottom: "0%",
          containLabel: true,
        },
        xAxis: {
          name: "身高",
          type: "category",
          data: dataH,
          nameGap: 25, // 表现为上下位置
          nameLocation: 'end',
          boundaryGap: false,

          splitLine: {
            show: false,
            interval: "auto",
            lineStyle: {
              color: ["#D4DFF5"],
            },
          },
          axisTick: {
            show: false,
          },
          axisLine: {
            lineStyle: {
              color: "#007F69",
            },
          },
          axisLabel: {
            margin: 10,
            textStyle: {
              fontSize: 12,
              color: "#444",
            },
          },
        },
        yAxis: [
          {
            type: "value",
            name: "退役人数",
            scale: true,
            axisLabel: {
              formatter: function (value, index) {
                console.log(index)
                return value.toFixed(1);
              },
              show: true,
              textStyle: {
                color: "#444",
              },
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: "#007F69",
              },
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              show: false,
              lineStyle: {
                color: "#fff",
              },
            },
            min: (value) => {
              // let num = 1 * (value.min.toString().length)
              return (value.min - 1).toFixed(1);
            },
            max: (value) => {
              console.log(value)
              // let num = 1 * (value.max.toString().length)
              // return value.max + 2;
            }
          },
          {
            type: "value",
            name: "现役人数",
            scale: true,
            axisLabel: {
              formatter: function (value, index) {
                console.log(index)
                return value.toFixed(1);
              },
              textStyle: {
                color: "#444",
              },
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: "#007F69",
              },
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              show: false,
              // lineStyle: {
              //   color: "#11366e",
              // },
            },
            min: (value) => {
              // let num = 1 * (value.min.toString().length)
              return (value.min - 1).toFixed(1);
            },
            max: (value) => {
              console.log(value)
              // let num = 1 * (value.max.toString().length)
              // return value.min + 50;
            }
          },
        ],
        series: [
          {
            name: "现役球员",
            type: "line",
            yAxisIndex: 1,
            smooth: true,
            showSymbol: false,
            symbol: "circle",
            symbolSize: 6,
            data: data_1,
            areaStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(
                    0,
                    0,
                    0,
                    1,
                    [
                      {
                        offset: 0,
                        color: "rgba(144, 225, 255, 1)"
                      },
                      {
                        offset: 1,
                        color: "rgba(245, 247, 247, 0)"
                      },
                    ],
                    false
                ),
              },
            },
            itemStyle: {
              normal: {
                color: "#76C7E9",
              },
            },
            lineStyle: {
              normal: {
                width: 1,
              },
            },
          },
          {
            name: "退役球员",
            type: "line",
            smooth: true,
            showSymbol: false,
            symbol: "circle",
            symbolSize: 6,
            data: data_2,
            areaStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(
                    0,
                    0,
                    0,
                    1,
                    [
                      {
                        offset: 0,
                        color: "rgba(0, 127, 105, 1)"
                      },
                      {
                        offset: 1,
                        color: "rgba(245, 247, 247, 0)"
                      },
                    ],
                    false
                ),
              },
            },
            itemStyle: {
              normal: {
                color: "#007F69",
              },
            },
            lineStyle: {
              normal: {
                width: 1,
              },
            },
          },
        ],
      };

      option && myChart.setOption(option);
    }
  }
}

</script>

<template>
  <div id="main-vis-1"></div>
</template>

<style scoped>
#main-vis-1 {
  height: 100%;
  width: 100%;
}
</style>